package com.androway.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.androway.model.AbstractModel.OnChangedListener;
import com.androway.model.ApplicationModel;
import com.androway.model.LoginModel;
import com.androway.services.Requester.RequestType;

public class LoginActivity extends Activity
{
	private LoginModel model;

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		Button button;

		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_login);
		this.model = new LoginModel();
		this.model.setListener(new OnChangedListener()
		{
			public void onSuccess(RequestType type, String service)
			{
				Toast.makeText(LoginActivity.this, "Welcome " + ApplicationModel.getConnected().getLogin(), Toast.LENGTH_LONG).show();
				Intent intent;

				intent = new Intent(getApplicationContext(), AppView.class);
				startActivity(intent);
			}

			public void onError(RequestType type, String errorMessage)
			{
				Toast.makeText(LoginActivity.this, errorMessage, Toast.LENGTH_LONG).show();
			}
		});
		button = (Button) findViewById(R.id.connectAction);
		button.setOnClickListener(onConnect());
	}

	public OnClickListener onConnect()
	{
		return (new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				String username;
				String password;
				EditText userEditText;
				EditText passwordText;

				userEditText = (EditText) findViewById(R.id.loginText);
				passwordText = (EditText) findViewById(R.id.passwdText);
				username = userEditText.getText().toString();
				password = passwordText.getText().toString();
				LoginActivity.this.model.connect(username, password);
			}
		});
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu)
	{
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_login, menu);
		return true;
	}

}
